package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<CategoryBrand> selectCategoryBrandList() {
        return categoryBrandMapper.selectCategoryBrandList();
    }

    @Override
    public void saveCategoryBrand(CategoryBrand categoryBrand) {
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId());
        long count = count(queryWrapper);
        if(count>0){
            throw new ServiceException("品牌分类关系已存在");
        }
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        baseMapper.insert(categoryBrand);
    }



    //根据三级分类ID查询品牌列表
//    @Override
//    public List<Brand> brandList(Long categoryId) {
//        //采用wrapper查询
//        List<CategoryBrand> categoryBrandList = this.list(new LambdaQueryWrapper<CategoryBrand>()
//                .eq(CategoryBrand::getCategoryId, categoryId)
//                .select(CategoryBrand::getBrandId));
//        if(!ObjectUtils.isEmpty(categoryBrandList)){
//            //采用Stream流获取品牌ID
//            List<Long> categoryIdList = categoryBrandList.stream()
//                    .map(CategoryBrand::getBrandId)
//                    .collect(Collectors.toList());
//            //根据品牌ID列表查询品牌列表
//            List<Brand> brandList = brandMapper.selectBatchIds(categoryIdList);
//            return brandList;
//        }
//        return null;
//    }

    //根据三级分类ID查询品牌列表
    @Override
    public List<Brand> brandList(Long categoryId) {
       //方式二:动态SQL
        return  baseMapper.brandList(categoryId);
    }
}
